package me.keiwu.user.controller;

import me.keiwu.pub.data.ConfigType;
import me.keiwu.pub.data.ResponseV2;
import me.keiwu.pub.data.config.ExploreConfigBean;
import me.keiwu.pub.util.CacheUtil;
import me.keiwu.pub.util.ContextUtil;
import me.keiwu.user.service.UserLbsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by kei on 27/02/2018.
 */
@RestController
@RequestMapping("/lbs")
public class UserLbsController {



    @Autowired
    private UserLbsService userLbsService;
    @Autowired
    private CacheUtil cacheUtil;


    @RequestMapping("update")
    public ResponseV2 updateLbs(HttpServletRequest request, Double latitude, Double longitude, Long cityCode, String cityName) {
        Long userId = ContextUtil.getLoginUserId(request);
        return ResponseV2.ok(userLbsService.updateLbs(userId, latitude, longitude, cityCode, cityName));
    }


    @RequestMapping("explore")
    public ResponseV2 nearUsers(HttpServletRequest request, Long cityCode, boolean refresh) {
        Long userId = ContextUtil.getLoginUserId(request);
        ExploreConfigBean config = cacheUtil.getConfig(ConfigType.EXPLORE_CONFIG, ExploreConfigBean.class);
        if (config == null) return ResponseV2.fail("缺少探索配置");
        int exploreCount = config.getInitExploreCount();
        exploreCount = (exploreCount <= 0 ? 1 : exploreCount) > 10 ? 10 : exploreCount;
        return ResponseV2.ok(userLbsService.fetchNearUserRandom(userId, cityCode, exploreCount, refresh));
    }
}
